package scau.jump.securitytrace.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import scau.jump.securitytrace.cache.ProductCache;
import scau.jump.securitytrace.cache.UserCache;
import scau.jump.securitytrace.service.SpringUtils;
import scau.jump.securitytrace.service.UserService;
import scau.jump.securitytrace.service.exception.IncorrectAccountOrPasswordException;
import scau.jump.securitytrace.ui.LoginFrame;
import scau.jump.securitytrace.ui.MainFrame;
import scau.jump.securitytrace.ui.MainFrame1;
import scau.jump.securitytrace.ui.MainFrame2;
import scau.jump.securitytrace.ui.MainFrameFactory;
import scau.jump.securitytrace.ui.ProductComboBoxModel;
import scau.jump.securitytrace.util.StringUtil;

import com.securitytrace.model.Role;
import com.securitytrace.model.User;

public class LoginFrameListener implements ActionListener{

	private static final Logger logger = Logger.getLogger(LoginFrameListener.class);
	
	private LoginFrame loginFrame;
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		logger.debug("点击登录按钮");
		String account = loginFrame.getAccountTextField().getText();
		char[] passwordChar= loginFrame.getPasswordField().getPassword();
		String password = String.valueOf(passwordChar);
		UserService userService = (UserService) SpringUtils.getBean("userService");
		
		try {
			User user = userService.login(account, password);
			UserCache.userMap.put(user.getAccount(), user);
			UserCache.accountMap.put("account", user.getAccount());
			MainFrame mainFrame = MainFrameFactory.getMainFrame();
			
			MainFrameStartButtonListener startButtonListener = new MainFrameStartButtonListener();
			MainFrameStopButtonListener  stopButtonListener = new MainFrameStopButtonListener();
			ProductComboBoxActionListener comboBoxListener = new ProductComboBoxActionListener();

			mainFrame.getAccountLabel().setText(user.getAccount());
			mainFrame.getAddressNameTextField().setText(user.getAddress().getAddressName());
			String addressOtherIfo = user.getAddress().getOtherInfo();
			mainFrame.getAddressOtherInfoTextPane().setText(StringUtil.splitAndNewline(addressOtherIfo));
			mainFrame.addStartButtonActionListener(startButtonListener);
			mainFrame.addStopButtonActionListener(stopButtonListener);
			
			if(mainFrame instanceof MainFrame1){
				mainFrame.getSelectProductComboBox().setModel(ProductComboBoxModel.getModel());
				String firstItem = (String) mainFrame.getSelectProductComboBox().getItemAt(0);
				String productOtherInfo = ProductCache.productOtherInfoMap.get(firstItem);
				mainFrame.getProductOtherInfoTextArea().setText(StringUtil.splitAndNewline(productOtherInfo));
				mainFrame.addProductComboBoxActionListener(comboBoxListener);	
			}
			
			loginFrame.dispose();
		} catch (IncorrectAccountOrPasswordException e1) {
			JOptionPane.showMessageDialog(null, "账号或者密码不正确，请重新登录！");
		}
	}
	
	public void setLoginFrame(LoginFrame loginFrame) {
		this.loginFrame = loginFrame;
	}

	public LoginFrame getLoginFrame() {
		return loginFrame;
	}

}
